package com.iplant.wms.serviceImpl.dao.bill;

import com.iplant.base.po.*;
import com.iplant.base.po.enums.ExceptionTypes;
import com.iplant.base.utils.annotation.dao.DBBaseDAO;
import com.iplant.base.utils.sql.BaseDAO;
import com.iplant.basic.service.po.bms.BMSEmployee;
import com.iplant.wms.service.po.bill.WMSOutboundOrderItem;
import com.iplant.wms.serviceImpl.dao.wms.WMSStockRecordOperateDAO;
import lombok.extern.slf4j.Slf4j;

import java.util.*;
import java.util.stream.Collectors;


@Slf4j
@DBBaseDAO(WMSOutboundOrderItem.class)
public class WMSOutboundOrderItemDAO extends BaseDAO {


    private static WMSOutboundOrderItemDAO Instance = null;

    private WMSOutboundOrderItemDAO() {
        super("");
        this.Init();
    }

    public static WMSOutboundOrderItemDAO getInstance() {
        if (Instance == null)
            Instance = new WMSOutboundOrderItemDAO();
        return Instance;
    }


    public int WMS_DeleteOutboundOrderItemList(BMSEmployee wLoginUser, List<WMSOutboundOrderItem> wItemList, OutResult<Integer> wErrorCode) {
        int wResult = 0;
        wErrorCode.set(0);

        try {

            wResult = this.DeletePOList(wLoginUser, wItemList, wErrorCode);

            if (wErrorCode.Result != 0)
                return wResult;
            List<Integer> wOperatorIDList = wItemList.stream().map(p -> p.StockOperateRecordID).distinct().collect(Collectors.toList());

            WMSStockRecordOperateDAO.getInstance().WMS_DeleteStockRecordOperateList(wLoginUser, wOperatorIDList, wErrorCode);
        } catch (Exception ex) {
            wErrorCode.set(ExceptionTypes.DBSQL.getValue());
            log.error("WMS_DeleteOutboundOrderItemList", ex);
        }
        return wResult;
    }



    @Override
    public <T extends BasePO> int DeletePOList(BaseUser wLoginUser, List<T> wBasePOList, OutResult<Integer> wErrorCode) {

        List<WMSOutboundOrderItem> wItemList = (List<WMSOutboundOrderItem>) wBasePOList;
        return this.WMS_DeleteOutboundOrderItemList((BMSEmployee)wLoginUser, wItemList, wErrorCode);
    }

    /**
     * 条件查询集合
     */
    public List<WMSOutboundOrderItem> WMS_SelectOutboundOrderItemList(BMSEmployee wLoginUser, int wOutboundOrderID, Pagination wPagination,
                                                                      OutResult<Integer> wErrorCode) {

        Map<String, Object> wParamMap = new HashMap<>();

        wParamMap.put("OutboundOrderID", wOutboundOrderID);
        return this.SelectPOList(wLoginUser, wParamMap, wPagination, wErrorCode);
    }


}
